package com.sky.controller.user;

import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.UserAddressService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequiredArgsConstructor
@RestController
@RequestMapping("/user/addressBook")
@Slf4j
public class UserAddressController {

    private final UserAddressService userAddressService;

    @PostMapping
    public Result<?> add(@RequestBody AddressBook addressBook){
        userAddressService.addAddress(addressBook);
        return Result.success();
    }

    @GetMapping("/list")
    public Result<?> list(){
        List<AddressBook> addressBookList = userAddressService.selectAllData();
        return Result.success(addressBookList);
    }

    @GetMapping("/default")
    public Result<?> getDefault(){
        List<AddressBook> addressBookDefaultList = userAddressService.selectDefaultData();
        return Result.success(addressBookDefaultList);
    }

    @PutMapping
    public Result<?> update(@RequestBody AddressBook addressBook){
        userAddressService.updateAddress(addressBook);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result<?> rowInfo(@PathVariable Long id){
        AddressBook addressBook = userAddressService.selectAddressInfoById(id);
        return Result.success(addressBook);
    }

    @DeleteMapping
    public Result<?> delete(Long id){
        userAddressService.deleteAddressById(id);
        return Result.success();
    }

    @PutMapping("/default")
    public Result<?> setDefault(@RequestBody AddressBook addressBook){
        userAddressService.setDefaultAddress(addressBook);
        return Result.success();
    }
}
